<template>
	<div class="coursewareSign w944">
		<alert ref="alert"></alert>
		<div class="label_top">
			<p>{{updateTitle}}</p>
			<em @click="reBack()">返回</em>
		</div>
		<div class="label_table">
			<div class="el-tabs-report">
				<div class="tongji">
					<span><b>{{baomingrenshu}}</b>报名人数</span>
					<span><b>{{yitongguo}}</b>已通过</span>
					<span><b>{{daishenhe}}</b>待审核</span>
				</div>
				<el-select v-model="auditStatus" placeholder="请选择审核状态" class="th_2">
					<el-option v-for="item in auditStatusOption" :key="item.id" :label="item.name" :value="item.id"></el-option>
				</el-select>
				<div class="msearch">
					<input placeholder="请输入学员名字……" v-model="search"/>
					<a @click="getTableList"><img src="../../assets/qudaoImg/search.png"/></a>
				</div>
			</div>
			<el-table ref="multipleTable" :data="tableData" border @selection-change="handleSelectionChange">
				<el-table-column label="序号" width="60">
					<template slot-scope="scope">{{ scope.row.sort}}</template>
				</el-table-column>
				<el-table-column prop="nickname" label="学员名字"></el-table-column>
				<el-table-column prop="auditStatus" label="审核状态">
					<template slot-scope="scope">
						<span v-if="scope.row.auditStatus==1">待审核</span>
						<span v-if="scope.row.auditStatus==2">已通过</span>
						<span v-if="scope.row.auditStatus==3">已拒绝</span>
					</template>
				</el-table-column>
				<el-table-column prop="createTime" label="报名时间"></el-table-column>
				<el-table-column label="操作"> 
					<template slot-scope="scope">
						<div v-if="scope.row.auditStatus==1">
							<el-button @click="handlePass(scope.row)">通过</el-button>
							<el-button @click="handleRefuse(scope.row)" class="red">拒绝</el-button>
						</div>
						<el-button @click="handleRefuse(scope.row)" v-if="scope.row.auditStatus==2" class="red">拒绝</el-button>
						<el-button @click="handlePass(scope.row)" v-if="scope.row.auditStatus==3">通过</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="fenyeMargin">
				<div class="pag_block">
					<el-pagination  @current-change="handleCurrentChange" :current-page="currentpage" :page-size="pagesize" layout="prev, pager, next" :total="total"></el-pagination>
				</div>
			</div>
		</div>
	</div>
</template>
<script>
	import alert from '../../components/Alert.vue';
	export default {
		data() {
			return {
				auditStatus:'',
				auditStatusOption:[{
					id:1,
					name:'待审核'
				},{
					id:2,
					name:'已审核'
				},{
					id:3,
					name:'已拒绝'
				}],
				baomingrenshu:'',
				yitongguo:'',
				daishenhe:'',
				search:'',
				sort:'',
				courseid:'',
				coursewareId:'', //课程编号id
				updateTitle:'', //课程名称
				coursewareType:'',//類型
		        pagesize: 15,
		        currentpage: 1,
		        total: 1,
				id:'',
				tableData: [],
				multipleSelection: [],
				deleteType:'delete',
			}
		},
		methods: {
			getParams() {
				this.courseid = this.$route.params.courseid;
				this.updateTitle = this.$route.params.updateTitle;
				console.log('courseWareSign:',this.$route.params);
			},
			getTableList(){
				let that = this;
				this.$ajax({
					url: this.$root.jifuUrl + "/mns/coursewareGroupBm/coursewareGroupBmUser",
					headers:{'ContentType': 'application/json; charset=utf-8'},
					method: 'POST',
					data: {
						courseid:that.courseid,
						page:that.currentpage,
						num:that.pagesize,
						auditStatus:that.auditStatus,
						nickName:that.search
					},
					success: res => {
						if(res.data.code == 200) {
							this.tableData = res.data.data.list;
							this.total=res.data.data.pageParam.items;
							for(var i = 0;i<this.tableData.length;i++){
								this.tableData[i].sort=i+1
							}
						}
					}
				});
			},
			getData(){
				let that = this;
				this.$ajax({
					url: this.$root.jifuUrl + "/mns/coursewareGroupBm/coursewareGroupBmStatistics",
					headers:{'ContentType': 'application/json; charset=utf-8'},
					method: 'POST',
					data: {courseid:this.courseid},
					success: res => {
						if(res.data.code == 200) {
							that.baomingrenshu = res.data.data.bmrenshu;
							that.yitongguo = res.data.data.tgrenshu;
							that.daishenhe = res.data.data.dsrenshu;
						}
					}
				});
			},
			handlePass(row){
				let that = this;
				this.$ajax({
					url: this.$root.jifuUrl + "/mns/coursewareGroupBm/coursewareGroupBmAuditUser",
					headers:{'ContentType': 'application/json; charset=utf-8'},
					method: 'POST',
					data: {
						auditStatus:2,
						id:row.id
					},
					success: res => {
						if(res.data.code == 200) {
							this.$rootFunction().addResSuccess();
						}else{
							this.failText = res.data.message;
							this.$rootFunction().deleteResFail();	
						}
					}
				});
			},
			handleRefuse(row){
				let that = this;
				this.$ajax({
					url: this.$root.jifuUrl + "/mns/coursewareGroupBm/coursewareGroupBmAuditUser",
					headers:{'ContentType': 'application/json; charset=utf-8'},
					method: 'POST',
					data: {
						auditStatus:3,
						id:row.id
					},
					success: res => {
						if(res.data.code == 200) {
							this.$rootFunction().addResSuccess();
						}else{
							this.failText = res.data.message;
							this.$rootFunction().deleteResFail();	
						}
					}
				});
			},
			toggleSelection(rows) {
				if(rows) {
					rows.forEach(row => {
						this.$refs.multipleTable.toggleRowSelection(row, !this.allSelect)
					})
					this.allSelect = !this.allSelect;
				}
			},
			handleSelectionChange(val) {
				this.multipleSelection = [];
				val.forEach(row => {
					this.multipleSelection.push(row.id);
				});
			},
			handleCurrentChange(value) {
				this.currentpage = value;
				this.getTableList();
			},
			reBack(){
				this.$router.push({
					name: 'courseware',
					params: {
						liveId:this.courseid,
						updateTitle:this.updateTitle
					}
				})
			}
		},
		mounted: function() {
			this.getParams();
			if(this.courseid==null){
				this.$store.commit('GET_courseWareSign');
				var s =this.$store.state.courseWareSign;
				this.courseid = s.courseid;
				this.updateTitle = s.updateTitle;
			}
			this.getTableList();
			this.getData();
			window.addEventListener("beforeunload",()=>{
				s={
					courseid:this.courseid,
					updateTitle:this.updateTitle
				}	
				this.$store.commit('SET_courseWareSign', s);
			});
		}
	}
</script>
